package com.gprinter.sample;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class IView extends View {
    private int pageOff = 10;
    private int coHeight = 0;
    private Bitmap bitmap;
    private Paint mPaint;
    private Canvas mCanvas;
    private int viewWidth;
    private int viewHeigth;
    float cl1Width = 0;
    Context context;
    public IView(Context context) {
        super(context);
        this.context = context;
    }

    public IView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public IView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        init(getMeasuredWidth(),getMeasuredHeight());
    }

    private void init(int width,int height){
        this.viewWidth = width;
        this.viewHeigth = height;

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setColor(Color.BLACK);
        mPaint.setStrokeWidth(3);
        bitmap = Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565);
        mCanvas= new Canvas(bitmap);
        coHeight = (height-pageOff*2 - 3*6)/5;
        drawbg1();
//        draw2();

//        draw3(qr);
    }

    public void draw2(){
        mCanvas.drawColor(Color.WHITE);
        mPaint.setStrokeWidth(3);
        mCanvas.drawLine(pageOff,pageOff,viewWidth-pageOff,pageOff,mPaint);
        mCanvas.drawLine(pageOff,pageOff,pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(pageOff,viewHeigth-pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(viewWidth-pageOff,pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);

        mPaint.setTextSize(90);

        float offsetH = 0;
        float width = mPaint.measureText("小");
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCanvas.drawText("小",pageOff+20,pageOff+width,mPaint);

        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCanvas.drawText("心",pageOff+20,pageOff+width*2+20,mPaint);

        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCanvas.drawText("易",pageOff+20,pageOff+width*3+20*2,mPaint);

        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mCanvas.drawText("碎",pageOff+20,pageOff+width*4+20*3,mPaint);

        offsetH = pageOff+20;
        float off = viewHeigth/8;

        String num = "0318012060";
        mPaint.setTextSize(25);
        mPaint.setStrokeWidth(1);
        float numWidth = mPaint.measureText(num);
        mCanvas.drawText(num,viewWidth-numWidth-60,offsetH+20,mPaint);

        float offWidth = pageOff+20+150;
        numWidth = mPaint.measureText("产品数量");

        float dis = 55;
        String text = "经销商";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis+20,viewWidth-pageOff,off+dis+20,mPaint);

        text = "地址";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis*2,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis*2+20,viewWidth-pageOff,off+dis*2+20,mPaint);

        text = "安装地址";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis*3,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis*3+20,viewWidth-pageOff,off+dis*3+20,mPaint);

        text = "产品类型";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis*4,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis*4+20,viewWidth-pageOff,off+dis*4+20,mPaint);

        text = "建议物流";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis*5,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis*5+20,viewWidth-pageOff,off+dis*5+20,mPaint);

        text = "产品数量";
        mCanvas.drawText(text,offWidth+numWidth-mPaint.measureText(text),off+dis*6,mPaint);
        mCanvas.drawLine(offWidth+numWidth,off+dis*6+20,viewWidth-pageOff,off+dis*6+20,mPaint);

        float lineWidth = viewWidth-5 - offWidth-numWidth;
        String jinxs = "盛百佳";
        mPaint.setTextSize(50);
        float xWidth = mPaint.measureText(jinxs);
        float startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        float startY = off+dis+20-xWidth/3/2;
        mCanvas.drawText(jinxs,startX,startY,mPaint);

        String addr = "北京市";
        mPaint.setTextSize(30);
        xWidth = mPaint.measureText(jinxs);
        startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        startY = off+dis*2+20-xWidth/addr.length()/2;
        mCanvas.drawText(addr,startX,startY,mPaint);

        float fontHeight = xWidth/addr.length()/2;

        String anzAddr = "炼油厂";
        xWidth = mPaint.measureText(anzAddr);
        startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        startY = off+dis*3+20-fontHeight;
        mCanvas.drawText(anzAddr,startX,startY,mPaint);

        String type = "X--303--8001--8001";
        xWidth = mPaint.measureText(type);
        startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        startY = off+dis*4+20-fontHeight;
        mCanvas.drawText(type,startX,startY,mPaint);

        String wuliu = "共14块";
        xWidth = mPaint.measureText(wuliu);
        startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        startY = off+dis*5+20-fontHeight;
        mCanvas.drawText(wuliu,startX,startY,mPaint);

        String count = "共1包，第1包";
        xWidth = mPaint.measureText(count);
        startX = offWidth+numWidth + lineWidth/2-xWidth/2;
        startY = off+dis*6+20-fontHeight;
        mCanvas.drawText(count,startX,startY,mPaint);
    }

    private void drawbg1(){
        mCanvas.drawColor(Color.WHITE);
        mCanvas.drawLine(pageOff,pageOff,viewWidth-pageOff,pageOff,mPaint);
        mCanvas.drawLine(pageOff,pageOff,pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(pageOff,viewHeigth-pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(viewWidth-pageOff,pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);

        mCanvas.drawLine(pageOff,coHeight+pageOff+3,viewWidth-pageOff,coHeight+pageOff+3,mPaint);
        mCanvas.drawLine(pageOff,coHeight*2+pageOff+3*2,viewWidth-pageOff,coHeight*2+pageOff+3*2,mPaint);
        mCanvas.drawLine(pageOff,coHeight*3+pageOff+3*3,viewWidth-pageOff,coHeight*3+pageOff+3*3,mPaint);
        mCanvas.drawLine(pageOff,coHeight*4+pageOff+3*4,viewWidth-pageOff,coHeight*4+pageOff+3*4,mPaint);

        mPaint.setTextSize(20);
        float width = mPaint.measureText("产品型号");
        mCanvas.drawLine(pageOff+3*2+width+4,pageOff,pageOff+3*2+width+4,viewHeigth-pageOff,mPaint);
        mPaint.setStrokeWidth(1);

        //单号
        float clumWidth = width + 4;
        cl1Width = clumWidth;

        float numWidth = mPaint.measureText("单号");

        float startX = clumWidth - numWidth+6+2;
        float startY = coHeight/2 + numWidth/4+pageOff;
        addText("单号",20,startX,startY);


        //联系人
        String text = "联系人";
        float lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*3/2+pageOff+lxWidth/3/2+3;
        addText(text,20,startX,startY);

        //产品型号
        text = "产品型号";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*2+coHeight/2+pageOff+lxWidth/4/2+3*2;
        addText(text,20,startX,startY);

        //终端地址
        text = "终端地址";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*3+coHeight/2+pageOff+lxWidth/4/2+3*3;
        addText(text,20,startX,startY);

        //产品数量
        text = "产品数量";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*4+coHeight/2+pageOff+lxWidth/4/2+3*4;
        addText(text,20,startX,startY);

        numWidth = addNum("0318100362");
        mPaint.setStrokeWidth(3);
        mCanvas.drawLine(clumWidth+3*2+ numWidth,pageOff,clumWidth+3*2+ numWidth,pageOff+coHeight,mPaint);

        text = "物流";
        mPaint.setStrokeWidth(1);
        mPaint.setTextSize(20);
        float textWidth = mPaint.measureText(text);
        mCanvas.drawText(text,clumWidth+3*2+ numWidth+3*2,pageOff+coHeight/2+textWidth/4-3,mPaint);

        float wlWidth = clumWidth+3*2+ numWidth+textWidth+3*2+3*2;
        mPaint.setStrokeWidth(3);
        mCanvas.drawLine(wlWidth,pageOff,wlWidth,pageOff+coHeight*2+3*2,mPaint);

        text = "工厂物流";
        mPaint.setStrokeWidth(1);
        mPaint.setTextSize(30);
        numWidth = mPaint.measureText(text);
        mCanvas.drawText(text,wlWidth+10,pageOff+coHeight/2+numWidth/4/2-3,mPaint);

        float bWidth = wlWidth - (pageOff+3*2+width+4);

        text = "居好家";
        mPaint.setTextSize(45);
        float widthj = mPaint.measureText(text);
        mCanvas.drawText(text,pageOff+3*2+width+4+bWidth/2-widthj/2,coHeight+pageOff+coHeight/2+widthj/3/2,mPaint);

        text = "吸塑门";
        mPaint.setTextSize(30);
        float widthx = mPaint.measureText(text);
        mCanvas.drawText(text,wlWidth+10,coHeight+pageOff+coHeight/2+widthx/3/2,mPaint);

        text = "100021-奥运橡木A-056";
        mPaint.setTextSize(30);
        float widthe = mPaint.measureText("奥运");
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*2+pageOff+coHeight/2+widthe/2/2+3,mPaint);

        text = "稔田黄总";
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*3+pageOff+coHeight/2+widthe/2/2+3*2,mPaint);

        text = "共 21 块，共 2 包，第 1 包";
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*4+pageOff+coHeight/2+widthe/2/2+3*3,mPaint);

        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        bitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
    }

    private void draw3(String qrMsg){
        Bitmap qrbit = QRCodeUtil.createQRImage(qrMsg,200,200);
        float qrWidth = qrbit==null?0: qrbit.getWidth()+20;
        float qrHeight = qrbit==null?0: qrbit.getHeight();

        mCanvas.drawColor(Color.WHITE);
        mCanvas.drawLine(pageOff,pageOff,viewWidth-pageOff,pageOff,mPaint);
        mCanvas.drawLine(pageOff,pageOff,pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(pageOff,viewHeigth-pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);
        mCanvas.drawLine(viewWidth-pageOff,pageOff,viewWidth-pageOff,viewHeigth-pageOff,mPaint);

        mCanvas.drawLine(pageOff,coHeight+pageOff+3,viewWidth-qrWidth,coHeight+pageOff+3,mPaint);
        mCanvas.drawLine(pageOff,coHeight*2+pageOff+3*2,viewWidth-pageOff,coHeight*2+pageOff+3*2,mPaint);
        mCanvas.drawLine(pageOff,coHeight*3+pageOff+3*3,viewWidth-pageOff,coHeight*3+pageOff+3*3,mPaint);
        mCanvas.drawLine(pageOff,coHeight*4+pageOff+3*4,viewWidth-pageOff,coHeight*4+pageOff+3*4,mPaint);

        mPaint.setTextSize(20);
        float width = mPaint.measureText("产品型号");
        mCanvas.drawLine(pageOff+3*2+width+4,pageOff,pageOff+3*2+width+4,viewHeigth-pageOff,mPaint);
        mPaint.setStrokeWidth(1);

        //单号
        float clumWidth = width + 4;
        cl1Width = clumWidth;

        float numWidth = mPaint.measureText("单号");

        float startX = clumWidth - numWidth+6+2;
        float startY = coHeight/2 + numWidth/4+pageOff;
        addText("单号",20,startX,startY);

        //联系人
        String text = "联系人";
        float lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*3/2+pageOff+lxWidth/3/2+3;
        addText(text,20,startX,startY);

        //产品型号
        text = "产品型号";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*2+coHeight/2+pageOff+lxWidth/4/2+3*2;
        addText(text,20,startX,startY);

        //终端地址
        text = "终端地址";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*3+coHeight/2+pageOff+lxWidth/4/2+3*3;
        addText(text,20,startX,startY);

        //产品数量
        text = "产品数量";
        lxWidth  = mPaint.measureText(text);

        startX = clumWidth - lxWidth+6+2;
        startY = coHeight*4+coHeight/2+pageOff+lxWidth/4/2+3*4;
        addText(text,20,startX,startY);

        numWidth = addNum("0318100362");
        mPaint.setStrokeWidth(3);

        text = "物流";
        mPaint.setStrokeWidth(1);
        mPaint.setTextSize(20);
        float textWidth = mPaint.measureText(text);

        float wlWidth = clumWidth+3*2+ numWidth+textWidth+3*2+3*2;
        mPaint.setStrokeWidth(3);
        mCanvas.drawLine(viewWidth-qrWidth,pageOff,viewWidth-qrWidth,pageOff+coHeight*2+3*2,mPaint);

        float bWidth = wlWidth - (pageOff+3*2+width+4);
        mPaint.setStrokeWidth(1);
        text = "居好家";
        mPaint.setTextSize(45);
        float widthj = mPaint.measureText(text);
        mCanvas.drawText(text,pageOff+3*2+width+4+bWidth/2-widthj/2,coHeight+pageOff+coHeight/2+widthj/3/2,mPaint);

        text = "100021-奥运橡木A-056";
        mPaint.setTextSize(30);
        float widthe = mPaint.measureText("奥运");
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*2+pageOff+coHeight/2+widthe/2/2+3,mPaint);

        text = "稔田黄总";
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*3+pageOff+coHeight/2+widthe/2/2+3*2,mPaint);

        text = "共 21 块，共 2 包，第 1 包";
        mCanvas.drawText(text,pageOff+3*2+width+4+10,coHeight*4+pageOff+coHeight/2+widthe/2/2+3*3,mPaint);

        mCanvas.drawBitmap(qrbit,viewWidth-qrWidth+8,pageOff+3+coHeight-qrHeight/2,mPaint);
    }

    private void addText(String text,int textSize,float startX,float startY){
        mPaint.setStrokeWidth(1);
        mPaint.setTextSize(textSize);
        mCanvas.drawText(text,startX,startY,mPaint);
    }

    public float  addNum(String num){
        int size = 30;
        float startX = 0;
        mPaint.setTextSize(size);
        float width = mPaint.measureText("00318100362") + 5*2;
        startX =cl1Width + pageOff+3*2+5*4;
        float startY = pageOff+3+coHeight/2+width/11/2;
        addText(num,size,startX,startY);
        return width+5*4;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap,0,0,mPaint);
    }



}
